package com.trainingcloud.ctrl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.trainingcloud.mapper.ResourceMapper;
import com.trainingcloud.mapper.TaskWithStudentMapper;
import com.trainingcloud.pojo.Resource;
import com.trainingcloud.pojo.Task;
import com.trainingcloud.pojo.TaskWithStudent;
import com.trainingcloud.pojo.User;
import com.trainingcloud.utils.MyFileUtil;
import com.trainingcloud.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 资源管理表 前端控制器
 * </p>
 *
 * @author 纸鹤
 * @since 2022-10-11
 */
@RestController
public class ResourceController {
    @Autowired
    private ResourceMapper resourceMapper;

    //获取资源
    @RequestMapping("/getResourcesByClass")
    public ResultUtil getResourcesByClass(Resource resource) {
        ResultUtil resultUtil = ResultUtil.ok();
        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("relation_class_id",resource.getRelationClassId());
        if(resource.getRelationTeacherId() != null){
            queryWrapper.eq("relation_teacher_id",resource.getRelationTeacherId());
        }
        List<Resource> resources = resourceMapper.selectList(queryWrapper);
        resultUtil.setData(resources);
        return resultUtil;
    }
    //增加资源
    @RequestMapping("/addResource")
    public ResultUtil addResource(MultipartFile file,Resource resource) {
        ResultUtil resultUtil = ResultUtil.ok();
        if (resource.getResourceType() == 0){
            if(file != null){
                String url = MyFileUtil.fileUpload("RESOURCE", file);
                resource.setResourceUrl(url);
            }
        }
        resourceMapper.insert(resource);
        return resultUtil;
    }
    //删除资源
    @RequestMapping("/delResources")
    public ResultUtil delResources(Resource resource) {
        ResultUtil resultUtil = ResultUtil.ok();
        if (resource.getResourceType() == 0){
            MyFileUtil.deleteFile(resource.getResourceUrl());
        }
        resourceMapper.deleteById(resource.getId());
        return resultUtil;
    }
}

